package com.example.provider1.controller;

import com.example.provider1.bean.City;
import com.example.provider1.service.CityService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/city")
public class CityController {
    @Autowired
    private CityService service;

    @RequestMapping("/findall")
    public List<City> findAll(){
        return service.findAll();
    }

    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping("/findbyname/{name}")
    public City findByName(@PathVariable String name) throws Exception {
        City city = service.findByName(name);
        if(city == null){
            throw new Exception();
        }else{
            return city;
        }
    }
    public City fallback(@PathVariable String name){
        City city = new City();
        city.setId(-1);
        city.setName("Not found");
        return city;
    }
}
